<?php
	session_start();
	ini_set("SMTP","93.115.117.60");
	require_once('recaptchalib.php');
	$publickey 	= "6LeMxgcAAAAAAJ6twEblW4fkiM-t7KW6GQpmiNpC";
	$privatekey = "6LeMxgcAAAAAAP2bxU4DIxoD3Kgk4AglV_k1r4c0";
 	
	include_once("functions_db.php");
	include 'headermenu.php';	
	function isValidEmail($email)
			{
			return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
			}
			
	$password_length = 9;

function make_seed() {
  list($usec, $sec) = explode(' ', microtime());
  return (float) $sec + ((float) $usec * 100000);
}

srand(make_seed());

$alfa = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
$token = "";
for($i = 0; $i < $password_length; $i ++) {
  $token .= $alfa[rand(0, strlen($alfa))];
}    


		
?>

<html> 

<head style="background-color:#C7EBFB;"> 
	<link rel="stylesheet" href="yui.css" type="text/css"> 
</head> 
<body> 

<div id="doc" class="yui-t7"> 
	   
			<div id="bd"> 
				<div class="yui-ge"> 
				  <div class="yui-u first"> 
					<!-- YOUR DATA GOES HERE --> 
					<table style="padding:30px" style="border-color:#C7EBFB;background-color:#C7EBFB;" >
					<tr style="border-color:#C7EBFB;background-color:#C7EBFB;"><td style="padding:30px" style="border-color:#C7EBFB;background-color:#C7EBFB;">
					<h3>  <p><b><big><center>Welcome to Methodology Platform!</center></big></b></p>
<p><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp This project design by Enterprise Concept consultants is a web application, which generates the project documentation according to a methodology. 
Generally, Project Management is about establishing tasks and sub-tasks, resources, objectives and deliverables.</b></p>
<p><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp The main objective of this application is generating the project steps, through the complex method (initially detailed planning of the project) and through the easy method (the next steps of the project will be define during the evolution of it).  </b></p>
<p><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp The functionalities of the project process are: User Management System, Tracking Management System, Document Management System, Documents Creation, Sharing Documents.</b></p></h3>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;					<img src="images/start3.jpg" alt="Easy like that" width="741" height="465"  border="0" align="middle" usemap="#Map" style="align:center">
					<map name="Map">
					  <area shape="rect" coords="186,84,321,114" href="companies.php">
                      <area shape="rect" coords="386,85,522,115" href="human_res.php">
				    <area shape="rect" coords="580,84,719,115" href="invitation.php">
					<area shape="circle" coords="80,101,41" href="index.php">
					</map>
					</td>
					</tr>
					</table>
					<?php
	                     
						dbconn();
			
						$message 			= "";
						$mailMessage 		= "";
						$passwordMessage 	= "";
						$confirmMessage 	= "";
						$firstnameMessage 	= "";
						$lastnameMessage 	= "";
						$newpasswordMessage = "";
						$mail 		= "";
						$first_name = "";
						$last_name 	= "";
			            $messagepass = "";
			        					
						//$temp = 0;
			            if(ISSET($_POST["passwordButton"]))
						{
						     $mail = $_POST["mail"];
								if($_POST["mail"] != null && $_POST["oldpassword"] != null &&$_POST["newpassword"] != null && $_POST["confirm_newpass"] != null && isValidEmail($_POST["mail"])!=null)
								{
								    $result = mysql_query("SELECT * FROM users WHERE MAIL='$mail'");
									if(mysql_num_rows($result) == 1)
										{    $oldpass = mysql_result($result, 0, "password");
											md5($_POST["oldpassword"]) 	== $oldpass; 
																	
												if($_POST["newpassword"] == $_POST["confirm_newpass"])
													{
														$sql = "UPDATE `users`  SET `password` = '".md5($_POST["newpassword"])."' WHERE MAIL ='$mail';";
														mysql_query($sql) or die(mysql_error()); 
													}
												else
													{
														echo "The New Password and Confirm New Password field must be the same!";
													}
										}
									else
										{
											//$temp = 1;
											$messagepass = "Wrong user name !";
							
								
										}
								}		
								else
								{ // $temp = 1;
								echo isValidEmail($_POST["mail"]);
									if(empty($_POST["mail"]))
										{ 
											$mailMessage = "Mail is required!";
										}
										else if (isValidEmail($_POST["mail"]))
										{
										$mailMessage = "Mail is incorrect!";
										}
			
									if(empty($_POST["oldpassword"]))
										{
											$passwordMessage = "Old Password is required!";			
										}
									if(empty($_POST["newpassword"]))
										{ 
											$newpasswordMessage = "New Password is required!";			
										}
									if(empty($_POST["confirm_newpass"]))
										{
											$confirmMessage = "Confirm New Password is required!";			
										}
									
								}	
                        }								
						else 
							{
							  //$temp = 1;
							}
						
			
			
						if(ISSET($_POST["registerButton"]))
						{
							$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

							$mail 		= $_POST["mail"];
							$first_name = $_POST["first_name"];
							$last_name 	= $_POST["last_name"];
				
							if ($resp->is_valid == TRUE)
							{
								if($_POST["mail"] != null && $_POST["password"] != null && $_POST["confirm_pass"] != null && $_POST["first_name"] != null && $_POST["last_name"] != null && isValidEmail($_POST["mail"])!=null)
								{
									if($_POST["password"] == $_POST["confirm_pass"])
									{
										$result = mysql_query("SELECT MAX(ACCOUNT)+1 AS acc FROM users");
										$newAccount = mysql_result($result, 0, "acc");
										$result = mysql_query("INSERT INTO users VALUES(null,'".$_POST['mail']."','".md5($_POST['password'])."','".$_POST['first_name']."','".$_POST['last_name']."','$newAccount','3');")or die(mysql_error());
										//echo "You have been registrated <br /> Now please click <a href=\"methodology.php\"> here </a> to connect";
										echo "<script type=\"text/javascript\">window.location.href=\"methodology.php\"</script>";
										exit();
									}
								else
									{
									echo "The password and confirm password field must be the same!";
									}
								}
								else
								{ //$temp = 1;
								echo isValidEmail($_POST["mail"]);
									if(empty($_POST["mail"]))
										{
											$mailMessage = "Mail is required!";
										}
										else if (isValidEmail($_POST["mail"]))
										{
										$mailMessage = "Mail is incorrect!";
										}
			
									if(empty($_POST["password"]))
										{
											$passwordMessage = "Password is required!";			
										}
									if(empty($_POST["confirm_pass"]))
										{
											$confirmMessage = "Confirm Password is required!";			
										}
									if(empty($_POST["first_name"]))
										{
											$firstnameMessage = "First Name is required!";			
										}
									if(empty($_POST["last_name"]))
										{
											$lastnameMessage = "Last name is required!";			
										}
								}					
							}
							else
								{
									echo "The reCAPTCHA wasn't entered correctly. Go back and try it again."."(reCAPTCHA said: " . $resp->error . ")"."<br/>";
								}
						}
					?>
	<?php
		$messagelog = "";
	    $forgetpass = "";
		$mailMessagelog = "";
		$passwordMessagelog = "";
	                                                                   
		$maillog = null;
		$passwordlog = null;
	
		//$temp = 0;
	
		if(ISSET($_POST["loginButton"]))
			{
				$maillog 		= $_POST["user"];
				$passwordlog 	= md5($_POST["pass"]);
	          
				if(empty($_POST["user"]) == false && empty($_POST["pass"]) == false)
					{
						$result = mysql_query("SELECT * FROM users WHERE MAIL='$maillog' AND PASSWORD='$passwordlog'");
				
						if(mysql_num_rows($result) == 1)
							{
								$_SESSION["userId"] 	= mysql_result($result, 0, "user_id");
								$_SESSION["mail"]		= mysql_result($result, 0, "mail");
								$_SESSION["account"]	= mysql_result($result, 0, "account");
								$_SESSION["priority"]	= mysql_result($result, 0, "priority");
								
								refreshPage('http://localhost/methodology/methodology.php');
							}
						else
							{
								//$temp = 1;
								
								$messagelog = "Wrong user name or password!";
							
							}	
						
							
					}
				else
					{
						//$temp = 1;
					
						if(empty($_POST["user"]))
							{
								$mailMessagelog = "user is required!";
							}
			
						if(empty($_POST["pass"]))
							{
								$passwordMessagelog = "Password is required!";			
							}
					}		
			}
				else
					{
						//$temp = 1;
					}
        	
								
		if(ISSET($_POST["passButton"]))
		{  
			$maillog = $_POST["user"];
			if(empty($_POST["user"]) == false)
			{
				$result = mysql_query("SELECT * FROM users WHERE MAIL ='$maillog'");
			   
				if(mysql_num_rows($result) == 1)
					{
						$sql = "UPDATE `users`  SET `password` = '".md5($token)."' WHERE MAIL ='$maillog';";
						mysql_query($sql) or die(mysql_error()); 
						echo $token;
						/*$recivedmess ='echo $passwordgen';
						$result = mail($_POST["mail"], "Subject: Invitation", $recivedmess, "From: office@enterprise-concept.com \r\n");
						if($result == TRUE)
						{
						echo "<p>Mail sent</p>";
						}
						else
						{
						echo "<p>Mail not sent</p>";
						} */
					}
					else
                    {
					$forgetpass = "Wrong user name!";
                    }										
										
			}
		}
				//if($temp == 1)
					

?>
						</div> 
						<div class="yui-u"> 	
<?php	
						
	
	if(ISSET($_SESSION["userId"]) == TRUE)
	{
		//##########################################################################
		//#### ESTE COMENTAT CA SA NU FACA CICLU ATUNCI CAND ESTE LOGAT USERUL! ####
		//##########################################################################
		
		//echo "<script type =\"text/javascript\">window.location.href=\"index.php\";</script>";
		
		exit();
	}
	else
	{
						?><form action="methodology.php" method="POST">
							<table style="border-color:#9CF;" width="300px">
							<tr> <td colspan=2 style="border-color:#9CF;" ><input type="Label" style="background:#2E3192; color:#FFFFFF; border-color:#2E3192;" value="LogIn Section">
							</td> </tr>
							<tr> <td style="border-color:#9CF;" width="100px"><b> User: </b> </td> <td width="150px" style="border-color:#9CF;"><input type="text" name="user" id="user" value="<?php echo $maillog; ?>"/>&nbsp;<?php echo $mailMessagelog; ?>       
							</td> </tr>
							<tr> <td style="border-color:#9CF;"><b> Password: </b></td> <td style="border-color:#9CF;"><input type="password" name="pass" id="pass" />&nbsp;<?php echo $passwordMessagelog; ?>
							</td> </tr>
							<tr> <td colspan=2 style="border-color:#9CF;">
							<input type="submit" name="loginButton" id="loginButton" value="LogIn" />&nbsp;<?php echo $messagelog;?>
							</td style="border-color:#9CF;"> </tr>
							<tr> <td colspan=2 style="border-color:#9CF;">
							<input type="submit" name="passButton" id="passButton" value="Generate new password" />&nbsp;<?php  echo $forgetpass ;?>
							</td style="border-color:#9CF;"> </tr>
							</table>
						 </form> <?
	}
   
?>
				<!-- YOUR DATA GOES HERE --> 
		
				<form name="Registry" method="POST" action="">			
					<p><b><input type="submit" name="RegisterSection" value="RegisterSection" style="background:#2E3192; color:#FFFFFF; border-color:#2E3192" value="Register Section"></b></p>
					
		<?php
					if(ISSET($_POST["RegisterSection"]))	
						{
		?>
							<table style="border-color:#9CF;" width="300px">
							<tr> <td style="border-color:#9CF;" width="100px">
									Mail <font color="#ff0000">*</font>
								</td> 
								<td style="border-color:#9CF;" width="150px">
									<input name="mail" id="mail" type="text" value="<?php echo $mail ?>">&nbsp;<?php echo $mailMessage ?>
								</td>
							</tr>
							<tr> <td style="border-color:#9CF;">					
									Password <font color="#ff0000">*</font> 
								</td> 
								<td style="border-color:#9CF;">
									<input name="password" id="password" type="password" value="">&nbsp;<?php echo $passwordMessage ?>
								</td>
							</tr>
							<tr>							
								<td style="border-color:#9CF;">
									Confirm Password <font color="#ff0000">*</font>
								</td> 
								<td style="border-color:#9CF;">	
									<input name="confirm_pass" id="confirm_pass" type="password" value="">&nbsp;<?php echo $confirmMessage ?>
								</td>
							</tr>
							<tr>						
								<td style="border-color:#9CF;">
									First name <font color="#ff0000">*</font>
								</td> 
								<td style="border-color:#9CF;">	
									<input name="first_name" id="first_name" type="text" value="<?php echo $first_name ?>">&nbsp;<?php echo $firstnameMessage ?>
								</td>
							</tr>
							<tr>						
								<td style="border-color:#9CF;">
									Last name <font color="#ff0000">*</font>
								</td> 
								<td style="border-color:#9CF;">	
									<input name="last_name" id="last_name" type="text" value="<?php echo $last_name ?>">&nbsp;<?php echo $lastnameMessage ?>
								</td>
							</tr>
							</table>
							<br>
							
									<?php echo recaptcha_get_html($publickey); ?>
								<br>
									<input type="submit" name="registerButton" id="registerButton" value="Register" />&nbsp;<?php echo $mailMessage ?>
							
								
							
		<?php
						}
		?>
		                    <p><b><input type="submit" name="ChangePassword" value="ChangePassword" style="background:#2E3192; color:#FFFFFF; border-color:#2E3192" value="Change Password "></b></p>
		<?php
					if(ISSET($_POST["ChangePassword"]))	
						{
		?>
							<table style="border-color:#9CF;" width="300px">
							<tr> <td style="border-color:#9CF;" width="100px">
									Mail <font color="#ff0000">*</font>
								</td> 
								<td style="border-color:#9CF;" width="150px">
									<input name="mail" id="mail" type="text" value="<?php echo $mail ?>"/>&nbsp;<?php echo $mailMessage ?>
								</td>
							</tr>
							<tr> <td style="border-color:#9CF;" width="100px">
									Old Password <font color="#ff0000">*</font>
								</td> 
								<td style="border-color:#9CF;" width="150px">
									<input name="oldpassword" id="oldpassword" type="text" value=""/>&nbsp;<?php echo $passwordMessage ?>
								</td>
							</tr>
							<tr> <td style="border-color:#9CF;">					
									NewPassword <font color="#ff0000">*</font>
								</td> 
								<td style="border-color:#9CF;">
									<input name="newpassword" id="nwepassword" type="password" value=""/>&nbsp;<?php echo $newpasswordMessage ?> 
									</td>
							</tr>
							<tr>							
								<td style="border-color:#9CF;">
									Confirm New Password <font color="#ff0000">*</font>
								</td> 
								<td style="border-color:#9CF;">	
									<input name="confirm_newpass" id="confirm_newpass" type="password" value=""/>&nbsp;<?php echo $confirmMessage ?>
									</td>
							</tr>
							</table>
							<br>
							<input type="submit" name="passwordButton" id="passwordButton" value="Ok" />&nbsp;<?php echo $mailMessage ?>
					<?php
						}
		            ?>
					
				</form>
	
	        </div> 
	</div> 
	
</div> 
</div>
   </body> 
</html>
<? include 'footer.php'; ?>